#include "DXUT.h"
#include "CRole.h"


void CRole::UpdateByWorldMatrix( const D3DXMATRIX * pWorld ,const D3DXVECTOR3&vOffset)
{
	vWorldRight.x= pWorld->_11;
	vWorldRight.y = pWorld->_12;
	vWorldRight.z= pWorld->_13;


	vWorldUp.x= pWorld->_21;
	vWorldUp.y = pWorld->_22;
	vWorldUp.z= pWorld->_23;




	vWorldAhead.x= pWorld->_31;
	vWorldAhead.y = pWorld->_32;
	vWorldAhead.z= pWorld->_33;



	r_pos.x= pWorld->_41;
	r_pos.y = pWorld->_42;
	r_pos.z= pWorld->_43;

	r_pos=r_pos+vOffset.x*vWorldRight + vOffset.y *vWorldUp + vOffset.z *vWorldAhead;

	r_rView(0,0)=vWorldRight.x;
	r_rView(0,1)=vWorldUp.x;
	r_rView(0,2)=vWorldAhead.x;

	r_rView(1,0)=vWorldRight.y;
	r_rView(1,1)=vWorldUp.y;
	r_rView(1,2)=vWorldAhead.y;



	r_rView(2,0)=vWorldRight.z;
	r_rView(2,1)=vWorldUp.z;
	r_rView(2,2)=vWorldAhead.z;
	r_rView(3,0)=	r_pos.x;
	r_rView(3,1)=	r_pos.y;
	r_rView(3,2)=	r_pos.z;
	r_rView(3,3)=1.0f;
	m_mViewInverse=*pWorld;

}

CRole::CRole(const TCHAR *resName,const TCHAR *szFile)
{

	pMesh=new CMesh(resName,szFile);
	Singleton<CGraphicsResourceManager>::Instance()->Add((void **)&pMesh);
	pMeshInstance=new CMeshInstance(pMesh);

	SetRolePos(&D3DXVECTOR3(0,0,0),&D3DXVECTOR3(0,0,10));
    //SetProjParams(D3DX_PI/4,1.0f,1.0f,1000.0f);
}

CRole::~CRole(void)
{
}



void CRole::SetRolePos(D3DXVECTOR3* pvEyePt, D3DXVECTOR3* pvLookatPt)
{
	if( NULL == pvEyePt || NULL == pvLookatPt )
		return;
	r_pos = *pvEyePt;
	r_vLookAt = *pvLookatPt;

	D3DXVECTOR3 vUp( 0,1,0 );
	D3DXMatrixLookAtLH( &r_rView, pvEyePt, pvLookatPt, &vUp );

	D3DXMATRIX mInvView;
	D3DXMatrixInverse( &mInvView, NULL, &r_rView );
	 
	
	   vWorldAhead   =D3DXVECTOR3(mInvView._31,mInvView._32,mInvView._33) ;
	   vWorldUp   = D3DXVECTOR3 (mInvView._21,mInvView._22,mInvView._23);
	   vWorldRight   =D3DXVECTOR3(mInvView._11,mInvView._12,mInvView._13);

	   pMeshInstance->SetPosition(r_pos);


}
void CRole::setRoleScale(D3DXVECTOR3* scale)
{
	 r_scale = *scale;
	 pMeshInstance->SetScale(r_scale);
}

void CRole::Yaw( float y )
{
	D3DXMATRIX Mat;
	D3DXMatrixRotationAxis(&Mat,&  vWorldUp  ,y);
	D3DXVec3TransformCoord(&vWorldAhead,&vWorldAhead,&Mat);
	D3DXVec3TransformCoord(&vWorldRight,&vWorldRight,&Mat);
}
void CRole::DefaultHandle( float fElapsedTime )
{
	float dt= fElapsedTime*10;

	if(Singleton<CInput>::Instance()->GetKeyboard()->IsKeyDown(DIK_W))
	{
		WalkAhead(dt );

	}

	if(Singleton<CInput>::Instance()->GetKeyboard()->IsKeyDown(DIK_S))
	{
		WalkAhead(-dt);

	}


	if(Singleton<CInput>::Instance()->GetKeyboard()->IsKeyDown(DIK_A))
	{
		WalkRight(-fElapsedTime*5);

	}



	if(Singleton<CInput>::Instance()->GetKeyboard()->IsKeyDown(DIK_D))
	{
		WalkRight(fElapsedTime*5);

	}


		
		
	if(Singleton<CInput>::Instance()->GetKeyboard()->IsKeyUp(DIK_Q))
	{
	 
		pMeshInstance->SetRotation(D3DXVECTOR3(0,pMeshInstance->m_rot.y+D3DX_PI/2,0));
		Yaw(fElapsedTime*-90);
	}

	if(Singleton<CInput>::Instance()->GetKeyboard()->IsKeyUp(DIK_E))
	{
	 
		pMeshInstance->SetRotation(D3DXVECTOR3(0,pMeshInstance->m_rot.y-D3DX_PI/2,0));
		Yaw(fElapsedTime*90);
	}
	//if(Singleton<CInput>::Instance()->GetKeyboard()->IsKeyDown(DIK_RIGHT))
	//{
	//Yaw(fElapsedTime);

	//}
	//if(Singleton<CInput>::Instance()->GetKeyboard()->IsKeyDown(DIK_UP))
	//{
	//	Pitch(-fElapsedTime);

	//}
	//if(Singleton<CInput>::Instance()->GetKeyboard()->IsKeyDown(DIK_DOWN))
	//{
	//	Pitch(fElapsedTime);

	//}

	//Yaw(0.05f*fElapsedTime*Singleton<CInput>::Instance()->GetMouse()->XMoved());
	//Pitch(0.05f*fElapsedTime*Singleton<CInput>::Instance()->GetMouse()->YMoved());
	//Zoom( -0.01f*fElapsedTime*Singleton<CInput>::Instance()->GetMouse()->ZMoved() );
}

void CRole::Render(float fElapsedTime)
{
	pMeshInstance->OnD3D9FrameRender(fElapsedTime);
}
void CRole::OnUpdate( double fTime, float fElapsedTime )
{
	//D3DXVec3Normalize(&vWorldAhead,&vWorldAhead);
	//D3DXVec3Cross(&vWorldUp,&vWorldAhead,&vWorldRight);
	//D3DXVec3Normalize(&vWorldUp,&vWorldUp);

	//D3DXVec3Cross(&vWorldRight,&vWorldUp,&vWorldAhead);

	//D3DXVec3Normalize(&vWorldRight,&vWorldRight);

	//float x=-D3DXVec3Dot(&vWorldRight,&r_pos);
	//float y=-D3DXVec3Dot(&vWorldUp,&r_pos);
	//float z=-D3DXVec3Dot(&vWorldAhead,&r_pos);

	//r_rView(0,0)=vWorldRight.x;
	//r_rView(0,1)=vWorldUp.x;
	//r_rView(0,2)=vWorldAhead.x;

	//r_rView(1,0)=vWorldRight.y;
	//r_rView(1,1)=vWorldUp.y;
	//r_rView(1,2)=vWorldAhead.y;



	//r_rView(2,0)=vWorldRight.z;
	//r_rView(2,1)=vWorldUp.z;
	//r_rView(2,2)=vWorldAhead.z;
	//r_rView(3,0)=x;
	//r_rView(3,1)=y;
	//r_rView(3,2)=z;
	//r_rView(3,3)=1.0f;
	//m_mViewInverse=
	//	D3DXMATRIX 
	//	(
	//	vWorldRight.x,vWorldRight.y,vWorldRight.z,0.0f,
	//	vWorldUp.x,vWorldUp.y,vWorldUp.z,0.0f,
	//	vWorldAhead.x,vWorldAhead.y,vWorldAhead.z,0.0f,
	//	x,y,z,1
	//	);
	//D3DXMatrixMultiply(&m_mViewProj,&r_rView,&m_mProj);

	 pMeshInstance->SetPosition(r_pos);
 
	
}